<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'setfa';
$return['status']=array(
		'0'=>'待付款_NO pay',
		'1'=>'待发货_NO send',
		'2'=>'待收货_NO get',
		'3'=>'已完成_Payment complete',
		'4'=>'已取消_exchange',
		'5'=>'申请退款',
		'6'=>'已退款',
		'7'=>'异常报备',
		'10'=>'拼单中',
		'11'=>'待售后接单',
		'12'=>'售后已接单',
		'13'=>'售后上门',
		'14'=>'售后异常',
		'15'=>'待售后',
		'16'=>'售后完成',
		'21'=>'部分发货',
		'-1'=>'已关闭',
);
if($_RQ['op']=='setfa'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$orderlist=$postdata['orderlist'];
		if($postdata['express_type0']){
			if(!$postdata['fatype'] && (!$postdata['express'] || !$postdata['expresssn'])){
				exi('请填写快递信息','error');
			}
			if($postdata['fatype'] && !$postdata['content']){
				exi('请填写物流信息','error');
			}
		}
		foreach($orderlist as $post){
			if(!$post['canfa']){
				exi('[平台不能发货]：由商户或供货商自己发货！','error');
			}
			$id=$post['id'];
			if(!$id){
				exi('参数错误！','error');
			}
			$post['fatype']=$postdata['fatype'];
			$post['express']=$postdata['express'];
			$post['expresssn']=$postdata['expresssn'];
			$post['content']=$postdata['content'];
			$post['peisong_id']=$postdata['peisong_id'];
			$post['need_setpeisong']=$postdata['need_setpeisong'];
			if($post['status']==2){
				$isup=true;
			}
			if($post['sendtype']==1 || $post['sendtype']==4){
				if(!$post['morefa']){
					$post['goodsinfo']=$post['goods'];
				}
				if($post['express_type']==0){
					$this->model('order')->order_send($id,$post,$isup);
				}elseif($post['express_type']==1){//平台配送
					if(!$post['peisong_id'] && $post['need_setpeisong']){
						exi('请选择配送员！','error');
					}
					if(!$post['peisong_id'] && $post['set_peisonguser']==1){
						$post['peisong_id']=model('jy_peisong/order')->order_pai($post);
						$post['order_pai']=1;
					}
					if($post['peisong_id']){
						$post['order_id']=$id;
						if(!$post['order_pai']){
							model('jy_peisong/order')->order_sendget($post);
						}
						$this->model('express')->updata($post);
					}else{
						$post['send_status']=12;
						cfc('jinyun_socket',$_SESSION['uniacid'])->send2room('刷新配送订单！','peisong_order');
						$this->model('order')->order_send($id,$post,$isup);
					}		
				}elseif($post['express_type']==2){//商家自己配送
					$this->model('order')->order_send($id,'',$isup);
				}elseif($post['express_type']==3){//达达配送
					$res=$this->model('order/dada_api')->dada_send($post);
					if($res['state']==1){
						$post['opensend_type']=1;
						$this->model('order')->order_send($id,$post,$isup);
					}else{
						exi($res['msg'],'error');
					}
				}elseif($post['express_type']==4){//货拉拉配送
					$huolalaorders[]=$post;
				}		
			}elseif($post['sendtype']==3){
				$this->model('order')->order_send_virtual($id,$post['content']);
			}elseif($post['sendtype']==2){
				$this->model('verify')->self_verify($post['verifycode'],0,$post['morefa']);
			}
		}
		if($huolalaorders){
			$res=model('jy_huolala/huo_api')->order_sendmore($huolalaorders);
			if($res['state']==1){
				foreach($huolalaorders as $order){
					$order['opensend_type']=2;
					$order['openorderid']=$res['openorderid'];
					if($order['status']==2){
						$isup=true;
					}
					$this->model('order')->order_send($order['id'],$order,$isup);
				}
			}else{
				exi($res['msg'],'error');
			}
		}
		exi('发货操作成功！','','/order.list/list/status=1');
	}
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择订单','error');
	}
	$citypeisong=$this->plugin_setting('citypeisong');
	$return['express']=$this->model('model')->express_types();
	$return['fatype']=array('快递配送','其他物流','无需物流');
	$orders['ids']=$ids;
	$orders['orderlist']=array();
	$htmls=array(
		'type'=>'edit',
		'desc'=>'合并发货，如果是自提核销，需单独输入核销码发货！快递发货，同城配送，可以合并订单发货，如果启用货拉拉，可以支持一路多单发货！',
	);
	$htmls['edit']=array();
	$allweight=0;
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			$item=$this->pdo_get('order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','status','is_peisong','dispatchid','dispatchprice','sendtype','storeid','merchid','address','addressid','ordersn','remark','price','isdish','weight'));
			$allweight +=$item['weight'];
		}
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			$item=$this->pdo_get('order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','status','is_peisong','dispatchid','dispatchprice','sendtype','storeid','merchid','address','addressid','ordersn','remark','price','isdish','weight'));
			$params=array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']);
			$ordergoods=pdo_getall('jy_weishop_order_goods',$params,array('orderid','status','optionid','total','goodsid','istaocan','api_pluginname','api_id','id'));
			$item['canfa']=0;
			$item['ordergoods']=array();
			foreach($ordergoods as &$gv){
				$gv['sendtype']=$item['sendtype'];
				$gv['refund_id']=$this->pdo_getcolumn('order_refund',array('ordersn'=>$item['ordersn'],'ordergoodsid'=>$gv['id']),'id');
				if($gv['refund_id']){
					$gv['noedit']=1;
				}
				$gv['title']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['goodsid']),'title');
				//商品规格
				if($gv['optionid']){
					$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs'));
					if($gv['istaocan']){
						$gv['selespename']=$options['title'];
					}else{
						$specs=$options['specs'];
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$gv['selespename']=implode("，",$specs);
					}
				}else{
					$gv['selespename']='无';
				}
				$gv['istotal']=$this->model('express')->getistotal($gv);
				$gv['lstotal']=$gv['total']-$gv['istotal'];
				$gv['canfa']=$gv['lstotal']>0?1:0;
				if($gv['canfa']){
					$item['canfa']=1;
				}
				$item['ordergoods'][$gv['id']]=$gv;
				$item['ordergoods'][$gv['id']]['title']=$gv['title'].'['.$gv['selespename'].']';
			}
			$item['goods']=$ordergoods;
			$order_express=pdo_get('jy_weishop_order_express',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'morefa'=>0));
			if($order_express){
				$item['express']=$order_express['express_id'];
				$item['expresssn']=$order_express['expresssn'];
				$item['opensend_type']=$order_express['opensend_type'];
				$item['fatype']=$order_express['fatype'];
				$item['content']=$order_express['content'];
				$item['morefa']=$order_express['morefa'];
			}
			if($item['sendtype']==1 || $item['sendtype']==4){
				$item['sendtitle']='运费'.$item['dispatchprice'].'元';
				if($item['dispatchid']){
					$sendtypetitle=pdo_getcolumn('jy_weishop_dispatch',array('id'=>$item['dispatchid'],'uniacid'=>$_SESSION['uniacid']),'title');
				}else{
					$sendtypetitle='快递';
				}
				$item['set_peisonguser']=$citypeisong['set_peisonguser'];
				$item['citypeisong_isopen']=$citypeisong['isopen'];
				$item['citypeisong_express_type']=$citypeisong['express_type']?:0;
				if($citypeisong['isopen']==1 || $item['isdish']){
					$item['express_type']=$citypeisong['express_type']?:1;
					if(!$item['isdish'] && $item['is_peisong']==2){
						$item['express_type']=0;
						$citypeisong['express_type']=0;
					}
					if($item['express_type']==3 && $citypeisong['hll_man'] && $allweight>=$citypeisong['hll_man']*1000){
						$item['express_type']=4;
						$citypeisong['express_type']=4;
					}
					if($item['express_type']==1){
						$tip='商品订单已打包好,由平台配送员配送！';
					}elseif($item['express_type']==2){
						$tip='商品订单已打包好,商家自己配送,无需配送员！';
					}elseif($item['express_type']==3){
						$tip='商品订单已打包好,同城配送！';
					}elseif($item['express_type']==4){
						$tip='商品订单已打包好,同城配送！';
					}elseif(!$item['express_type']){
						$tip='商品订单已打包好,快递发货！';
						$item['sendtitle'] .='('.$sendtypetitle.')';
					}
				}else{
					$item['express_type']=0;
					$tip='商品订单已打包好,快递发货！';
					$item['sendtitle'] .='('.$sendtypetitle.')';
				}
				$item['address']=json_decode($item['address'],true);
				if($item['address'] && ($item['address']['address'] || $item['address']['mobile'])){
					$address=$item['address'];
					$addressid=$item['address'];
				}else{
					$address=pdo_get('core_address',array('id'=>$item['addressid']));
					$addressid=$item['addressid'];
				}
				$item['address']=array(
					'address'=>model('core/member')->get_address_str($addressid),
					'mobile'=>$address['mobile'],
					'name'=>$address['name'],
				);
				$item['fatype']=0;
				$item['morefa']=0;
				$htmls['edit'][]=array('type'=>'help','title'=>'【'.$item['ordersn'].'】订单：'.$tip);
				$htmls['edit'][]=array('type'=>'tip','title'=>'[平台不能发货]：由商户或供货商自己发货！','show'=>array('canfa'.$id=>0));
				$htmls['edit'][]=array('type'=>'copy','title'=>'运费','name'=>'sendtitle'.$id);
				$htmls['edit'][]=array('type'=>'copy','title'=>'联系人','name'=>'address'.$id.'.name');
				$htmls['edit'][]=array('type'=>'copy','title'=>'联系电话','name'=>'address'.$id.'.mobile');
				$htmls['edit'][]=array('type'=>'copy','title'=>'收货地址','name'=>'address'.$id.'.address');
				$htmls['edit'][]=array('type'=>'array','name'=>'goods'.$id,'title'=>'订单商品详情','noadd'=>1,'nodel'=>1,'nosplice'=>1,'noedit'=>1,'help'=>'[<red>无需发货</red>的商品]：一般是商户发货或者供货商发货！',
						'list'=>array(
							array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
							array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
							array('type'=>'string','title'=>'总量','name'=>'total','read'=>true),
							array('type'=>'string','title'=>'剩余量','name'=>'lstotal','read'=>true),
							array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
							array('type'=>'radio','title'=>'是否需要发货','name'=>'canfa','options'=>'canfa'),
						),
						'details'=>array(
							array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
							array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
							array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
							array('type'=>'radio','title'=>'是否需要发货','name'=>'canfa','options'=>'canfa'),
						),
				);
				if($item['canfa']==1){
					$needfa=1;
					if(!$item['express_type']){
						$express_type0=1;
					}
					if($item['express_type']==1){
						$express_type1=1;
					}
					if($item['express_type']==2){
						$express_type2=1;
					}
					if($item['express_type']==3){
						$express_type3=1;
					}
					if($item['express_type']==4){
						$express_type4=1;
					}
				}
			}else{
				$item['verifycode']=pdo_getcolumn('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']),'number');
				$item['morefa']=0;
				$tip=$item['sendtype']==3?'虚拟发货':'物品自提';
				$htmls['edit'][]=array('type'=>'help','title'=>'【'.$item['ordersn'].'】订单：'.$tip);
				$htmls['edit'][]=array('type'=>'array','name'=>'goods'.$id,'title'=>'商品详情','noadd'=>1,'nosplice'=>1,'noedit'=>1,
						'list'=>array(
							array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
							array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
							array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
						),
						'details'=>array(
							array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
							array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
							array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'.$id=>2)),
							array('type'=>'string','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'.$id=>3),'help'=>'虚拟物品发货，填写发货内容！'),
						),
				);
				$htmls['edit'][]=array('type'=>'string','title'=>'自提核销码','name'=>'verifycode'.$id,'show'=>array('sendtype'.$id=>2));
				$htmls['edit'][]=array('type'=>'text','title'=>'虚拟物品发货内容','name'=>'content'.$id,'show'=>array('sendtype'.$id=>3),'help'=>'虚拟物品发货，填写发货内容！');
			}
			$orders['orderlist'][]=$item;
			foreach($item as $k=>$v){
				$orders[$k.$id]=$v;
			}
			$peisongs=array();
			//多商户用户，选择平台区域配送员
			if($item['merchid']){
				$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>0));
			}else{
				$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>array($item['storeid'],0)));
			}
			foreach($areas as $v){
				$users=pdo_getall('jy_peisong_member',array('uniacid'=>$_SESSION['uniacid'],'group_id'=>$v['id'],'status'=>1),array('mid','title'));
				foreach($users as $uv){
					$peisongs[]=$uv;
				}
			}
			$return['peisongs'.$id]=$peisongs;
		}
	}
	if($needfa){
		if($express_type0){
			$orders['express_type0']=1;
			$htmls['edit'][]=array('type'=>'help','title'=>'多订单合并物流发货，自提核销的除外！');
			$htmls['edit'][]=array('type'=>'radio','title'=>'物流选择','name'=>'fatype','options'=>'fatype');
			$htmls['edit'][]=array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('fatype'=>0));
			$htmls['edit'][]=array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('fatype'=>0));
			$htmls['edit'][]=array('type'=>'string','title'=>'物流内容','name'=>'content','show'=>array('fatype'=>array(1,2)));
		}
		if($express_type1){
			$orders['express_type1']=1;
			$htmls['edit'][]=array('type'=>'help','title'=>'多订单合并同城配送，自提核销的除外！');
			$htmls['edit'][]=array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定配送员？','help'=>'不指定，则由配送员自己抢单，或系统指派！');
			$htmls['edit'][]=array('type'=>'select','title'=>'指定配送员','name'=>'peisong_id','options'=>'peisongs','option_value'=>'mid','show'=>array('need_setpeisong'=>1),'help'=>'只能指定该门店下面的配送员，或本城市的平台配送员！');
		}
		if($citypeisong['express_type']==3){
			$orders['express_type3']=1;
			$htmls['edit'][]=array('type'=>'help','title'=>'多订单合并同城配送（发送给达达配送！【慎重操作】：确定要呼叫《达达配送员》过来拿货吗?），自提核销的除外！');
		}
		if($citypeisong['express_type']==4){
			$orders['express_type4']=1;
			$htmls['edit'][]=array('type'=>'help','title'=>'多订单合并同城配送,发送给货拉拉配送！【慎重操作】：确定要呼叫《货拉拉配送员》过来拿货吗?），自提核销的除外！');
		}
	}
	$return['canfa']=array('无需发货','需要发货');
	$return['htmls']=$htmls;
	$return['item']=$orders;
	exi($return);
}
?>